package com.buybuyyi.stylistcrm.security;

import com.buybuyyi.stylistcrm.security.dao.UserDao;
import com.buybuyyi.stylistcrm.security.rest.DefaultUserDetails;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import stylistcrm.core.services.security.dto.OperationAuthorityDto;
import stylistcrm.core.services.security.dto.UserDto;
import stylistcrm.core.services.security.dto.ViewAuthorityDto;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yanwc on 2016/9/10.
 */
@Service
public class DefaultUserDetailsService implements UserDetailsService {
    private static Logger logger = Logger.getLogger(DefaultUserDetailsService.class);

    @Autowired
    UserDao userDao;

    @Transactional(readOnly=true)
    public UserDetails loadUserByUsername(String ssoId)
            throws UsernameNotFoundException {
        UserDto user = userDao.findUserByUserName(ssoId);
        System.out.println("UserDto : " + user);
        if (user == null) {
            String msg = "UserName:" + ssoId + " Not found";
            logger.info(msg);
            System.out.println(msg);
            throw new UsernameNotFoundException(msg);
        } else {
            String msg = "UserName:" + ssoId + " login successful";
            logger.info("UserDto : " + msg);
        }
        DefaultUserDetails rev = new DefaultUserDetails(user.getLoginName(), user.getPassword(),
                user.isEnable(), true, true, !user.isLock(), getGrantedAuthorities(user));
        rev.setUserDto(user);
        return rev;
    }

    private List<GrantedAuthority> getGrantedAuthorities(UserDto user){

        List<GrantedAuthority> authorities = new ArrayList<>();

        List<OperationAuthorityDto> listOperAuth = user.getListOperationAuthority();
        if(listOperAuth != null) {
            authorities.addAll(listOperAuth.stream().map(o -> new SimpleGrantedAuthority("ROLE_" + o.getCode())).collect(Collectors.toList()));
        }
        List<ViewAuthorityDto> listViewAuthor = user.getListViewAuthority();
        if(listViewAuthor != null){
            authorities.addAll(listViewAuthor.stream().map(v -> new SimpleGrantedAuthority("ROLE_" + v.getCode())).collect(Collectors.toList()));
        }
        System.out.print("authorities :"+authorities);

        GrantedAuthority ga = new SimpleGrantedAuthority("ROLE_"+"app.goods.release.audit");
        authorities.add(ga);
        return authorities;
    }

}
